package common.post.service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import common.customer.bean.Customer;
import common.post.bean.Contacts;
import common.post.bean.SInfo;
import common.post.dao.ContactsDao;
import common.post.dao.SInfoDao;
import common.system.bean.Area;
import common.system.bean.City;
import common.util.CommonCode;

public class SInfoServiceImpl implements SInfoService {
	private SInfoDao sInfoDao;
	private ContactsDao contactsDao;
	@Override
	public int addNewGirl(City city, Area area, Customer customer,
			String desc, String images, String title, String phone, String qq) {
		Set<Contacts> set = new HashSet<Contacts>();
		if(phone!=null && !"".equals(phone.trim())){
			Contacts c = new Contacts();
			c.setContent(phone);
			c.setTypeName(CommonCode.CONNTCTS_TYPE_TELPHONE);
			int b=contactsDao.save(c);
			if(b>0){
				set.add(c);				
			}
		}
		if(qq!=null && !"".equals(qq.trim())){
			Contacts c = new Contacts();
			c.setContent(qq);
			c.setTypeName(CommonCode.CONNTCTS_TYPE_QQ);
			int b=contactsDao.save(c);
			if(b>0){
				set.add(c);				
			}
		}
		SInfo sinfo =new SInfo();
		sinfo.setContent(desc);
		sinfo.setPics(images);
		sinfo.setContacts(set);
		sinfo.setCity(city);
		sinfo.setArea(area);
		sinfo.setTitle(title);
		sinfo.setPublisher(customer);
		sinfo.setVip(false);
		sinfo.setChaping(0);
		sinfo.setClick(0);
		sinfo.setHaoping(0);
		sinfo.setPublishTime(System.currentTimeMillis());
		sinfo.setReply(0);
		sinfo.setTuijian(false);
		sinfo.setType(CommonCode.SINFO_TYPE_LOUFENG);
		sinfo.setZhongping(0);
		int result = sInfoDao.save(sinfo);
		return result;
	}
	@Override
	public List<SInfo> getSInfosExcludeType(int cityId, int areaId,
			String type, int page, int pageSize, int days, String orderBy,
			String jinghua, String tuijian, boolean vip) {
		String queryString="from common.post.bean.SInfo s " +
				"where s.type!='"+type+"'" +
				" and s.city.id="+cityId +"";
		if (days > 0) {
			queryString = queryString + " and " + System.currentTimeMillis()
					+ "-s.publishTime <=" + (days * 24 * 3600000l);
		}
		if (jinghua != null && "jinghua".equals(tuijian.trim())) {
			queryString = queryString + " and s.haoping-s.chaping>=20";
		}
		if (tuijian != null && "tuijian".equals(tuijian.trim())) {
			queryString = queryString + " and s.tuijian=true";
		}
		if (areaId > 0) {
			queryString = queryString + " and s.area.id=" + areaId;
		}
		if (!vip) {
			queryString = queryString + " and s.vip=0";
		}
		if (orderBy != null && !"".equals(orderBy.trim())) {
			queryString = queryString + " order by s." + orderBy + " desc";
		}
		List<SInfo> list = this.sInfoDao.find(queryString, page, pageSize);
		return (List<SInfo>) list;
	}
	@Override
	public long countSInfoExcludeType(int cityId, int areaId, String type,
			int days, String jinghua, String tuijian, boolean vip) {
		String queryString="select count(s) from common.post.bean.SInfo s " +
				"where s.type!='"+type+"'" +
				" and s.city.id="+cityId +"";
		if (jinghua != null && "jinghua".equals(tuijian.trim())) {
			queryString = queryString + " and s.haoping-s.chaping>=20";
		}
		if (days > 0) {
			queryString = queryString + " and " + System.currentTimeMillis()
					+ "-s.publishTime <=" + (days * 24 * 3600000l);
		}
		if (areaId > 0) {
			queryString = queryString + " and s.area.id=" + areaId;
		}
		if (tuijian != null && "tuijian".equals(tuijian.trim())) {
			queryString = queryString + " and s.tuijian=true";
		}
		if(!vip){
			queryString = queryString +" and s.vip=0";
		}
		return  (Long)this.sInfoDao.find(queryString,true).get(0);
	}
	@Override
	public SInfo getSInfoById(int id) {
		SInfo sinfo =this.sInfoDao.get(id);
		sinfo.setClick((sinfo.getClick()+1));
		this.sInfoDao.update(sinfo);
		return sinfo;
	}
	@Override
	public List<SInfo> getSInfos(int cityId, int areaId, String type, int page,
			int pageSize, int days, String orderBy,
			String jinghua,
			String tuijian,boolean vip) {
		String queryString="from common.post.bean.SInfo s " +
								"where s.type='"+type+"'" +
								" and s.city.id="+cityId +"";
		if(days > 0){
			queryString=queryString+" and "+System.currentTimeMillis()+"-s.publishTime <="+(days*24*3600000l);
		}
		if(jinghua!=null && "jinghua".equals(tuijian.trim())){
			queryString=queryString+" and s.haoping-s.chaping>=20";
		}
		if(tuijian!=null && "tuijian".equals(tuijian.trim())){
			queryString=queryString+" and s.tuijian=true";
		}
		if(areaId > 0){
			queryString=queryString+" and s.area.id="+areaId;
		}
		if(!vip){
			queryString=queryString+" and s.vip=0";
		}
		if(orderBy!=null && !"".equals(orderBy.trim())){
			queryString=queryString+" order by s."+orderBy+" desc";
		}
		System.out.println("queryString============="+queryString);
		List<SInfo> list = this.sInfoDao.find(queryString,page,pageSize);
		return (List<SInfo>)list;
	}
	@Override
	public long countSInfo(int cityId, int areaId, String type, int days,
			String jinghua,
			String tuijian,boolean vip) {
		String queryString="select count(s) from common.post.bean.SInfo s " +
				"where s.type='"+type+"'" +
				" and s.city.id="+cityId +"";
		if (jinghua != null && "jinghua".equals(tuijian.trim())) {
			queryString = queryString + " and s.haoping-s.chaping>=20";
		}
		if (days > 0) {
			queryString = queryString + " and " + System.currentTimeMillis()
					+ "-s.publishTime <=" + (days * 24 * 3600000l);
		}
		if (areaId > 0) {
			queryString = queryString + " and s.area.id=" + areaId;
		}
		if (tuijian != null && "tuijian".equals(tuijian.trim())) {
			queryString = queryString + " and s.tuijian=true";
		}
		if(!vip){
			queryString = queryString +" and s.vip=0";
		}
		System.out.println("queryString=============coount-----------"+queryString);
		return  (Long)this.sInfoDao.find(queryString,true).get(0);
	}
	/*@SuppressWarnings("unchecked")
	@Override
	public List<SInfo> getSInfoByCityId(int cityId) {
		@SuppressWarnings("rawtypes")
		List list = this.sInfoDao.find("from common.post.bean.SInfo s where s.city.id="+cityId);
		return (List<SInfo>)list;
	}*/

	/*@SuppressWarnings("unchecked")
	@Override
	public List<SInfo> getSInfoByCityIdAndAreaId(int cityId, int areaId) {
		@SuppressWarnings("rawtypes")
		List list = this.sInfoDao.find("from common.post.bean.SInfo s where s.city.id="+cityId +" and where s.area.id="+areaId);
		return (List<SInfo>)list;
	}*/

	/*@SuppressWarnings("unchecked")
	@Override
	public List<SInfo> getSInfoByCityIdAndAreaId(int cityId, int areaId,
			String type) {
		@SuppressWarnings("rawtypes")
		List list = this.sInfoDao.find("from common.post.bean.SInfo s where s.type='"+type+"' and s.city.id="+cityId +" and where s.area.id="+areaId);
		return (List<SInfo>)list;
	}*/
	/*@Override
	public List<SInfo> getSInfoByPageAndPageSize(int cityId, int areaId,
			String type, int page, int pageSize) {
		return null;
	}*/
	@Override
	public boolean addSInfo(SInfo sinfo) {
		sinfo.setPublishTime(System.currentTimeMillis());
		int id = sInfoDao.save(sinfo);
		if(id>0){
			return true;
		}
		return false;
	}

	@Override
	public boolean updateSInfo(SInfo sinfo) {
		try{
			this.sInfoDao.update(sinfo);
			return true;
		}catch(Exception e){
			e.printStackTrace();
		}
		return false;
	}

	/*@SuppressWarnings("unchecked")
	@Override
	public List<SInfo> getAllSInfo() {
		@SuppressWarnings("rawtypes")
		List list = sInfoDao.find("from common.post.bean.SInfo");
		return (List<SInfo>)list;
	}*/
	
	//******************getter and setter method
	public SInfoDao getsInfoDao() {
		return sInfoDao;
	}

	public void setsInfoDao(SInfoDao sInfoDao) {
		this.sInfoDao = sInfoDao;
	}
	public ContactsDao getContactsDao() {
		return contactsDao;
	}
	public void setContactsDao(ContactsDao contactsDao) {
		this.contactsDao = contactsDao;
	}
	
}
